package com.nari.kmd_dataservice.service.impl;

import com.google.gson.reflect.TypeToken;
import com.mysql.jdbc.StringUtils;
import com.nari.core.tool.utils.RedisUtil;
import com.nari.kmd_dataservice.constant.ConstantParameter;
import com.nari.kmd_dataservice.mapper.ServerMapper;
import com.nari.kmd_dataservice.pojo.dto.common.CommonResponseDTO;
import com.nari.kmd_dataservice.pojo.dto.laRegister.LaRegisterDto;
import com.nari.kmd_dataservice.pojo.dto.reportResource.ReportResourceDto;
import com.nari.kmd_dataservice.pojo.dto.responseCons.ResponseConsDto;
import com.nari.kmd_dataservice.pojo.dto.responseCons.ResponseConsListDataDto;
import com.nari.kmd_dataservice.pojo.dto.targetHistory.TargetHistoryDto;
import com.nari.kmd_dataservice.pojo.dto.targetHistory.TargetHistoryListDataDto;
import com.nari.kmd_dataservice.pojo.dto.targetResponseCons.TargetResponseConsDto;
import com.nari.kmd_dataservice.pojo.dto.targetResponseCons.TargetResponseConsListDataDto;
import com.nari.kmd_dataservice.pojo.dto.upLoadUserInfo.UpLoadUserInfoDto;
import com.nari.kmd_dataservice.pojo.dto.upLoadUserInfo.UpLoadUserInfoListDataDto;
import com.nari.kmd_dataservice.pojo.entity.*;
import com.nari.kmd_dataservice.pojo.request.laRegister.LaRegisterListDataRequest;
import com.nari.kmd_dataservice.pojo.request.laRegister.LaRegisterRequest;
import com.nari.kmd_dataservice.pojo.request.reportResource.ReportResourceDeviceInfo;
import com.nari.kmd_dataservice.pojo.request.reportResource.ReportResourceListDataRequest;
import com.nari.kmd_dataservice.pojo.request.reportResource.ReportResourceRequest;
import com.nari.kmd_dataservice.pojo.request.responseCons.ResponseConsRequest;
import com.nari.kmd_dataservice.pojo.request.targetHistory.TargetHistoryRequest;
import com.nari.kmd_dataservice.pojo.request.targetResponseCons.TargetResponseConsRequest;
import com.nari.kmd_dataservice.pojo.request.upLoadUserInfo.UpLoadUserInfoRequest;
import com.nari.kmd_dataservice.service.ServerService;
import com.nari.kmd_dataservice.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: ServerServiceImpl
 * @Description: impl jiekou ceng
 * @Author: LZL
 * @Date: 2022/01/25 10:01
 * @Version 1.0.0
 */
@Service
@Slf4j
public class ServerServiceImpl implements ServerService {
    @Resource
    ServerMapper serverMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public String laRegister(String jsonParameter) {

        LaRegisterRequest laRegisterRequest = GsonUtils.fromJson(jsonParameter,new TypeToken<LaRegisterRequest>(){}.getType());

        LaRegisterDto laRegisterDto = new LaRegisterDto();

        boolean checkOk = true;
        String errStr = "";
        User user = serverMapper.selectUser(laRegisterRequest.getUserName(), laRegisterRequest.getPassword());
        if(user==null){
            log.error("用户{}不存在",laRegisterRequest.getUserName());
            laRegisterDto.setStatus("02");
            checkOk = false;
            errStr = "参数错误1";
        }
        else{
            log.info("user="+user);
            log.info("运行数据-> {}", laRegisterRequest.toString());
            if(!user.getPassword().equals(laRegisterRequest.getPassword())){
                log.error("密码{}不正确",laRegisterRequest.getPassword());
                laRegisterDto.setStatus("02");
                checkOk = false;
                errStr = "参数错误2";
            }
//            else if(user.getLastLoginTime()!=null && laRegisterRequest.getRegistRationId()==null){
//                log.error("无注册ID");
//                laRegisterDto.setStatus("02");
//                checkOk = false;
//                errStr = "参数错误3";
//            }
            else if(laRegisterRequest.getUrl()==null){
                log.error("无URL");
                laRegisterDto.setStatus("02");
                checkOk = false;
                errStr = "参数错误4";
            }
//            else if(!NetworkUtil.checkUrl(laRegisterRequest.getUrl())){
//                log.error("URL格式不正确");
//                laRegisterDto.setStatus("02");
//                checkOk = false;
//                errStr = "参数错误5";
//            }
            else{
                String registRationId = SM3.encrypt(laRegisterRequest.getUserName()+ConstantParameter.registerKey);
                if(!StringUtils.isNullOrEmpty(laRegisterRequest.getRegistRationId()) && !registRationId.equals(laRegisterRequest.getRegistRationId())){
                    log.error("注册ID不正确");
                    laRegisterDto.setStatus("02");
                    checkOk = false;
                    errStr = "参数错误6";
                }
                else{
                    if(!CollectionUtils.isEmpty(laRegisterRequest.getData())){
                        List<String> consNoList = new ArrayList<>();
                        List<DeviceInfo> deviceInfoList = new ArrayList<>();
                        for(int i=0; i<laRegisterRequest.getData().size(); i++){
                            LaRegisterListDataRequest laRegisterListDataRequest = laRegisterRequest.getData().get(i);
                            String consNo = laRegisterListDataRequest.getConsNo();
                            if(!consNoList.contains(consNo)){
                                consNoList.add(consNo);
                            }
                            else{
                                checkOk = false;
                                errStr = "注册携带响应负荷用户重复";
                                errStr = "参数错误7";
                                break;
                            }
                            if(!CollectionUtils.isEmpty(laRegisterListDataRequest.getDeviceInfo())){
                                for(ReportResourceDeviceInfo reportResourceDeviceInfo:laRegisterListDataRequest.getDeviceInfo()){
                                    try{
                                        DeviceInfo deviceInfo = new DeviceInfo();
                                        deviceInfo.setConsNo(laRegisterListDataRequest.getConsNo());
                                        deviceInfo.setDevType(reportResourceDeviceInfo.getType());
                                        deviceInfo.setDevNum(Integer.valueOf(reportResourceDeviceInfo.getAmount()));
                                        deviceInfoList.add(deviceInfo);
                                    }catch (Exception e){
                                        log.error("数据异常",e);
                                    }
                                }
                            }
                        }
                        if(checkOk){
                            List<String> existRespondConsNoList = checkExistRespondConsList(registRationId, consNoList);
                            boolean addConsNotExist = true; //向用户档案表增加不存在用户
                            boolean addResponseNotExist = true; //向响应用户表增加不存在用户
                            List<String> consNoExistList = getConsNoListFromCons(consNoList);
                            if(consNoExistList==null || consNoExistList.size()!=consNoList.size()){
                                log.error("增加响应负荷用户无档案");
                                checkOk = false;
                                errStr = "增加响应负荷用户无档案";
                                errStr = "参数错误8";

                                if(addConsNotExist){
                                    log.error("增加无档案用户");
                                    checkOk = true;
                                    errStr = "";
                                    List<String> consNoAddList = new ArrayList<>();
                                    consNoAddList.addAll(consNoList);
                                    if(consNoExistList!=null){
                                        consNoAddList.removeAll(consNoExistList);
                                    }
                                    addConsNoListToCons(consNoAddList);
                                }
                            }
                            if(addResponseNotExist){
                                if(CollectionUtils.isEmpty(existRespondConsNoList) || existRespondConsNoList.size() != laRegisterRequest.getData().size()){
                                    List<ReportResourceListDataRequest> addList = new ArrayList<>();
                                    laRegisterRequest.getData().forEach(item->{
                                        if(existRespondConsNoList==null || !existRespondConsNoList.contains(item.getConsNo())){
                                            if(item.getConsNo().length()==10 && item.getConsNo().startsWith("0")){
                                                ReportResourceListDataRequest reportResourceListDataRequest = new ReportResourceListDataRequest();
                                                reportResourceListDataRequest.setConsNo(item.getConsNo());
                                                reportResourceListDataRequest.setPeakAbility(item.getPeakAblity());
                                                reportResourceListDataRequest.setVallyAbility(item.getVallyAblity());
                                                reportResourceListDataRequest.setPowerType(item.getPowerType());
                                                reportResourceListDataRequest.setResponseLevel(item.getResponseLevel());
                                                addList.add(reportResourceListDataRequest);
                                            }
                                        }
                                    });
                                    if(!CollectionUtils.isEmpty(addList)){
                                        addRespondConsList(registRationId, addList, "0");
                                    }
                                }
                            }
//                            else{
//                                if(existRespondConsNoList==null || existRespondConsNoList.size()!=consNoList.size()){
//                                    log.error("注册携带响应负荷用户不存在");
//                                    checkOk = false;
//                                    errStr = "注册携带响应负荷用户不存在";
//                                    errStr = "参数错误9";
//                                }
//                            }
                            if(!CollectionUtils.isEmpty(deviceInfoList)){
                                log.info("注册插入设备信息");
                                addAndUpdate(deviceInfoList);
                            }
                        }
                    }
                    if(checkOk){
                        laRegisterDto.setStatus("01");
                        String token = JwtUtils.getToken(laRegisterRequest);
                        laRegisterDto.setToken(token);
                        laRegisterDto.setRegistRationId(registRationId);

                        user.setLaName(laRegisterRequest.getLaName());
                        user.setUrl(laRegisterRequest.getUrl());
                        user.setRegistRationId(registRationId);
                        user.setLastLoginTime(new Date());
                        serverMapper.updateUser(user);
                        redisUtil.set(laRegisterRequest.getUserName(), token, ConstantParameter.localTokenOutMinute*60);
                    }
                    else{
                        laRegisterDto.setStatus("02");
                        return GsonUtils.toJsonWtihNullField(new CommonResponseDTO("02", errStr));
                    }
                }
            }
        }
        if(checkOk){
            return GsonUtils.toJsonWtihNullField(laRegisterDto);
        }
        else{
            laRegisterDto.setStatus("02");
            return GsonUtils.toJsonWtihNullField(new CommonResponseDTO("02", errStr));
        }
    }

    private List<String> checkExistRespondConsList(String registRationId, List<String> consNoList) {
        List<String> existRespondConsNoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(consNoList)) {
            List<List<String>> objListList = ObjConvertListUtil.splitArrayList(consNoList, 800);
            for(List<String> objList:objListList){
                List<String> consNoExistListTmp = serverMapper.checkExistRespondConsList(registRationId, objList);
                if (!CollectionUtils.isEmpty(consNoExistListTmp)) {
                    existRespondConsNoList.addAll(consNoExistListTmp);
                }
            }
        }
        return existRespondConsNoList;
    }

    private void addRespondConsList(String registRationId, List<ReportResourceListDataRequest> addList, String status) {
        if(!CollectionUtils.isEmpty(addList)){
            List<List<ReportResourceListDataRequest>> objListList = ObjConvertListUtil.splitArrayList(addList, 800);
            for(List<ReportResourceListDataRequest> objList:objListList){
                serverMapper.addRespondConsList(registRationId, objList, status);
            }
        }
    }

    private List<String> getConsNoListFromCons(List<String> consNoList) {
        List<String> consNoExistList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(consNoList)) {
            List<List<String>> objListList = ObjConvertListUtil.splitArrayList(consNoList, 800);
            for(List<String> objList:objListList){
                List<String> consNoExistListTmp = serverMapper.getConsNoListFromCons(objList);
                if (!CollectionUtils.isEmpty(consNoExistListTmp)) {
                    consNoExistList.addAll(consNoExistListTmp);
                }
            }
        }
        return consNoExistList;
    }

    private void addConsNoListToCons(List<String> consNoAddList) {
        if (!CollectionUtils.isEmpty(consNoAddList)) {
            List<List<String>> objListList = ObjConvertListUtil.splitArrayList(consNoAddList, 800);
            for(List<String> objList:objListList){
                serverMapper.addConsNoListToCons(objList);
            }
        }
    }

    private void addAndUpdate(List<DeviceInfo> deviceInfoList) {
        List<String> consNoList = new ArrayList<>();
        List<DeviceInfo> deviceInfoListDb = new ArrayList<>();
        List<DeviceInfo> deviceInfoListDbDele = new ArrayList<>();

        //查询户号设备
        for(DeviceInfo deviceInfo:deviceInfoList){
            if(!consNoList.contains(deviceInfo.getConsNo())){
                consNoList.add(deviceInfo.getConsNo());
            }
        }
        List<List<String>> consNoListList = ObjConvertListUtil.splitArrayList(consNoList, 800);
        if (!CollectionUtils.isEmpty(consNoListList)) {
            for(List<String> consNoListTemp:consNoListList){
                List<DeviceInfo> deviceInfoListDbTmp = serverMapper.getDeviceInfoList(consNoListTemp);
                if (!CollectionUtils.isEmpty(deviceInfoListDbTmp)) {
                    deviceInfoListDb.addAll(deviceInfoListDbTmp);
                }
            }
        }
        //删除不存在的设备
        if (!CollectionUtils.isEmpty(deviceInfoListDb)) {
            for(int i=0; i<deviceInfoListDb.size(); i++){
                DeviceInfo deviceInfo_i = deviceInfoListDb.get(i);
                boolean exist = false;
                for(int j=0; j<deviceInfoList.size(); j++){
                    DeviceInfo deviceInfo_j = deviceInfoList.get(j);
                    if(deviceInfo_j.getConsNo().equals(deviceInfo_i.getConsNo()) && deviceInfo_j.getDevType().equals(deviceInfo_i.getDevType())){
                        exist = true;
                        if(deviceInfo_j.getDevNum()==deviceInfo_i.getDevNum()){
                            deviceInfoList.remove(j);
                            j--;
                        }
                        break;
                    }
                }
                if(!exist){
                    deviceInfoListDbDele.add(deviceInfo_i);
                    deviceInfoListDb.remove(i);
                    i--;
                }
            }
        }
        if (!CollectionUtils.isEmpty(deviceInfoListDbDele)) {
            log.info("删除设备");
            List<List<DeviceInfo>> deviceInfoListDeleList = ObjConvertListUtil.splitArrayList(deviceInfoListDbDele, 800);
            for(List<DeviceInfo> deviceInfoListDele:deviceInfoListDeleList){
                serverMapper.deleteDeviceInfoList(deviceInfoListDele);
            }
        }
        //添加更新
        if (!CollectionUtils.isEmpty(deviceInfoList)) {
            log.info("更新设备");
            List<List<DeviceInfo>> deviceInfoListUpdateList = ObjConvertListUtil.splitArrayList(deviceInfoList, 800);
            for(List<DeviceInfo> deviceInfoListUpdate:deviceInfoListUpdateList){
                serverMapper.addDeviceInfoList(deviceInfoListUpdate);
            }
        }
        else{
            log.info("需要更新设备为空");
        }
    }

    @Override
    public String reportResource(String jsonParameter) {

        ReportResourceRequest reportResourceRequest = GsonUtils.fromJson(jsonParameter,new TypeToken<ReportResourceRequest>(){}.getType());

        ReportResourceDto reportResourceDto = new ReportResourceDto();

        boolean checkOk = true;
        boolean addConsNotExist = true; //向用户档案表增加不存在用户
        boolean updateResponseCons = true; //
        boolean updateResponseConsToPass = false; //
        String errStr = "";
        User user = serverMapper.selectUserByID(reportResourceRequest.getRegistRationId());
        if(user==null){
            log.error("注册ID{}用户不存在", reportResourceRequest.getRegistRationId());
            checkOk = false;
            errStr = "注册ID用户不存在";
            reportResourceDto.setStatus("02");
        }
        else{
            log.info("运行数据-> {}", reportResourceRequest.toString());
            if(!CollectionUtils.isEmpty(reportResourceRequest.getData())){
                List<ReportResourceListDataRequest> addList = new ArrayList<>();
                List<DeviceInfo> deviceInfoList = new ArrayList<>();
                List<ReportResourceListDataRequest> deleteList = new ArrayList<>();
                for(int i=0; i<reportResourceRequest.getData().size(); i++){
                    ReportResourceListDataRequest reportResourceListDataRequest = reportResourceRequest.getData().get(i);
                    if("01".equals(reportResourceListDataRequest.getUpdateStatus())){
                        if(deleteList.contains(reportResourceListDataRequest)){
                            log.error("既增加也删除{}", reportResourceListDataRequest);
                            checkOk = false;
                            errStr += ";既增加也删除";
                            break;
                        }
                        if(!addList.contains(reportResourceListDataRequest)){
                            addList.add(reportResourceListDataRequest);
                        }
                        if(!CollectionUtils.isEmpty(reportResourceListDataRequest.getDeviceInfo())){
                            for(ReportResourceDeviceInfo reportResourceDeviceInfo:reportResourceListDataRequest.getDeviceInfo()){
                                try{
                                    DeviceInfo deviceInfo = new DeviceInfo();
                                    deviceInfo.setConsNo(reportResourceListDataRequest.getConsNo());
                                    deviceInfo.setDevType(reportResourceDeviceInfo.getType());
                                    deviceInfo.setDevNum(Integer.valueOf(reportResourceDeviceInfo.getAmount()));
                                    deviceInfoList.add(deviceInfo);
                                }catch (Exception e){
                                    log.error("数据异常",e);
                                }
                            }
                        }
                    }
                    else if("02".equals(reportResourceListDataRequest.getUpdateStatus())){
                        if(addList.contains(reportResourceListDataRequest)){
                            log.error("既删除也增加{}", reportResourceListDataRequest);
                            checkOk = false;
                            errStr += ";既删除也增加";
                            break;
                        }
                        if(!deleteList.contains(reportResourceListDataRequest)){
                            deleteList.add(reportResourceListDataRequest);
                        }
                    }
                    else{
                        log.error("未知更新类型{}", reportResourceListDataRequest);
                        checkOk = false;
                        errStr += ";未知更新类型";
                        break;
                    }
                }
                if(checkOk){
                    if(!CollectionUtils.isEmpty(addList) && checkOk){
                        List<String> consNoList = new ArrayList<>();
                        for(int i=0; i<addList.size(); i++){
                            consNoList.add(addList.get(i).getConsNo());
                        }
                        List<String> existRespondConsNoList = checkExistRespondConsList(reportResourceRequest.getRegistRationId(), consNoList);
                        if(!CollectionUtils.isEmpty(existRespondConsNoList)){
                            log.error("增加响应负荷用户已存在");
                            checkOk = false;
                            errStr += ";增加响应负荷用户已存在";
                            if(updateResponseCons){
                                checkOk = true;
                                errStr = "";
                                List<ReportResourceListDataRequest> existList = new ArrayList<>();
                                for(int i=0; i<addList.size(); i++){
                                    ReportResourceListDataRequest addResource =  addList.get(i);
                                    if(existRespondConsNoList.contains(addResource.getConsNo())){
                                        if(addResource.getConsNo().length()==10 && addResource.getConsNo().startsWith("0")){
                                            existList.add(addResource);
                                        }
                                        addList.remove(i);
                                        i--;
                                    }
                                }
                                if(!CollectionUtils.isEmpty(existList)){
                                    log.error("更新响应负荷用户");
                                    //更新为审核通过 //TODO
                                    if(!CollectionUtils.isEmpty(existList)){
                                        List<List<ReportResourceListDataRequest>> objListList = ObjConvertListUtil.splitArrayList(existList, 800);
                                        for(List<ReportResourceListDataRequest> objList:objListList){
                                            serverMapper.updateRespondConsListPass(reportResourceRequest.getRegistRationId(), objList, updateResponseConsToPass?"true":"false");
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if(!CollectionUtils.isEmpty(deleteList) && checkOk){
                        List<String> consNoList = new ArrayList<>();
                        for(int i=0; i<deleteList.size(); i++){
                            consNoList.add(deleteList.get(i).getConsNo());
                        }
                        List<String> existRespondConsNoList = checkExistRespondConsList(reportResourceRequest.getRegistRationId(), consNoList);
                        if(existRespondConsNoList==null || existRespondConsNoList.size()!=deleteList.size()){
                            log.error("删除响应负荷用户不存在");
                            checkOk = false;
                            errStr += ";删除响应负荷用户不存在";
                        }
                    }
                    if(!CollectionUtils.isEmpty(addList) && checkOk){
                        List<String> consNoList = new ArrayList<>();
                        for(int i=0; i<addList.size(); i++){
                            consNoList.add(addList.get(i).getConsNo());
                        }
                        List<String> consNoExistList = getConsNoListFromCons(consNoList);
                        if(consNoExistList==null || consNoExistList.size()!=addList.size()){
                            log.error("增加响应负荷用户无档案");
                            checkOk = false;
                            errStr += ";增加响应负荷用户无档案";
                            if(addConsNotExist){
                                log.error("增加无档案用户");
                                checkOk = true;
                                errStr = "";
                                List<String> consNoAddList = new ArrayList<>();
                                consNoAddList.addAll(consNoList);
                                if(consNoExistList!=null){
                                    consNoAddList.removeAll(consNoExistList);
                                }
                                addConsNoListToCons(consNoAddList);
                            }
                        }
                    }
                    if(checkOk){
                        if(!CollectionUtils.isEmpty(addList) && checkOk){
                            log.info("增加响应负荷用户");
                            addRespondConsList(reportResourceRequest.getRegistRationId(), addList, "0");
                        }
                        if(!CollectionUtils.isEmpty(deleteList) && checkOk){
                            log.info("删除响应负荷用户");
                            List<List<ReportResourceListDataRequest>> objListList = ObjConvertListUtil.splitArrayList(deleteList, 800);
                            for(List<ReportResourceListDataRequest> objList:objListList){
                                serverMapper.deleteRespondConsList(reportResourceRequest.getRegistRationId(), objList);
                            }
                        }
                    }
                    if(!CollectionUtils.isEmpty(deviceInfoList)){
                        log.info("插入设备信息");
                        addAndUpdate(deviceInfoList);
                    }
                }
            }
            else{
                log.error("无更新数据");
                errStr += ";无更新数据";
                checkOk = false;
            }

            if(checkOk){
                reportResourceDto.setStatus("01");
            }
            else{
                reportResourceDto.setStatus("02");
                return GsonUtils.toJsonWtihNullField(new CommonResponseDTO("02", errStr));
            }
        }

        return GsonUtils.toJsonWtihNullField(reportResourceDto);
    }

    @Override
    public String responseCons(String jsonParameter) {
        ResponseConsRequest responseConsRequest = GsonUtils.fromJson(jsonParameter, ResponseConsRequest.class);
        ResponseConsDto responseConsDto = new ResponseConsDto();

        User user = serverMapper.selectUserByID(responseConsRequest.getRegistRationId());
        if(user==null){
            log.error("注册ID{}用户不存在", responseConsRequest.getRegistRationId());
            responseConsDto.setStatus("02");
        }
        else{
            log.info("运行数据-> {}", responseConsRequest.toString());
            responseConsDto.setStatus("01");
            List<ResponseConsListDataDto> responseUsers = serverMapper.selectResponseConsList(responseConsRequest.getRegistRationId());
            responseConsDto.setResponseUsers(responseUsers);
        }
        return GsonUtils.toJsonWtihNullField(responseConsDto);
    }
    @Override
    public String targetResponseCons(String jsonParameter) {

        TargetResponseConsRequest targetResponseConsRequest  = GsonUtils.fromJson(jsonParameter,new TypeToken<TargetResponseConsRequest>(){}.getType());

        TargetResponseConsDto targetResponseConsDto = new TargetResponseConsDto();

        User user = serverMapper.selectUserByID(targetResponseConsRequest.getRegistRationId());
        if(user==null){
            log.error("注册ID{}用户不存在", targetResponseConsRequest.getRegistRationId());
            targetResponseConsDto.setStatus("02");
            return GsonUtils.toJsonWtihNullField(new CommonResponseDTO("02", "注册ID用户不存在"));
        }
        else{
            log.info("运行数据-> {}", targetResponseConsRequest.toString());
            boolean verifyPara = true; //是否验证参数
            if(verifyPara){
                ResponseEvent responseEvent = serverMapper.getResponseEventByEventID(targetResponseConsRequest.getEventId());
                if(responseEvent==null){
                    return GsonUtils.toJsonWtihNullField(new CommonResponseDTO("02", "事件编号不存在"));
                }
                else{
                    if(targetResponseConsRequest.getData()!=null){
                        targetResponseConsDto.setStatus("01");
                        List<String> consNoList = serverMapper.getResponseEventRangeConsNoList(targetResponseConsRequest.getRegistRationId(), targetResponseConsRequest.getEventId());
                        List<TargetResponseConsListDataDto> responseUsers = new ArrayList<>();
                        targetResponseConsRequest.getData().forEach(item ->{
                            TargetResponseConsListDataDto targetResponseConsListDataDto = new TargetResponseConsListDataDto();
                            targetResponseConsListDataDto.setConsNo(item.getConsNo());
                            if(consNoList!=null && consNoList.contains(item.getConsNo())){
                                targetResponseConsListDataDto.setIsSuccess("01");
                            }
                            else{
                                targetResponseConsListDataDto.setIsSuccess("02");
                                targetResponseConsListDataDto.setReason("事件不存在该用户");
                            }
                            responseUsers.add(targetResponseConsListDataDto);
                        });
                        targetResponseConsDto.setResponseUsers(responseUsers);
                    }
                    else{
                        return GsonUtils.toJsonWtihNullField(new CommonResponseDTO("02", "无户号"));
                    }
                }
            }
            else{
                targetResponseConsDto.setStatus("01");
                //判断date是否为空 如果不为空则对数据进行遍历 依次加入返回参数中
                if(!CollectionUtils.isEmpty(targetResponseConsRequest.getData())){
                    List<TargetResponseConsListDataDto> responseUsers = new ArrayList<>();
                    targetResponseConsRequest.getData().forEach(item ->{
                        TargetResponseConsListDataDto targetResponseConsListDataDto = new TargetResponseConsListDataDto();
                        targetResponseConsListDataDto.setConsNo(item.getConsNo());
                        targetResponseConsListDataDto.setIsSuccess("01");
                        responseUsers.add(targetResponseConsListDataDto);
                    });
                    targetResponseConsDto.setResponseUsers(responseUsers);
                }
            }
        }

        return GsonUtils.toJsonWtihNullField(targetResponseConsDto);
    }

    @Override
    public String targetHistory(String jsonParameter) {

        TargetHistoryRequest targetHistoryRequest = GsonUtils.fromJson(jsonParameter,new TypeToken<TargetHistoryRequest>(){}.getType());

        TargetHistoryDto targetHistoryDto = new TargetHistoryDto();

        User user = serverMapper.selectUserByID(targetHistoryRequest.getRegistRationId());
        if(user==null){
            log.error("注册ID{}用户不存在", targetHistoryRequest.getRegistRationId());
            targetHistoryDto.setStatus("02");
            return GsonUtils.toJsonWtihNullField(new CommonResponseDTO("02", "注册ID用户不存在"));
        }
        else{
            log.info("运行数据-> {}", targetHistoryRequest.toString());
            boolean verifyPara = true; //是否验证参数
            if(verifyPara){
                ResponseEvent responseEvent = serverMapper.getResponseEventByEventID(targetHistoryRequest.getEventId());
                if(responseEvent==null){
                    return GsonUtils.toJsonWtihNullField(new CommonResponseDTO("02", "事件编号不存在"));
                }
                else{
                    targetHistoryDto.setStatus("01");
                    List<TargetHistoryListDataDto> data = new ArrayList<>();
                    List<ResponseEventRange> responseEventRangeList = serverMapper.getResponseEventRangeList(targetHistoryRequest.getRegistRationId(), targetHistoryRequest.getEventId());
                    responseEventRangeList.forEach(item->
                    {
                        TargetHistoryListDataDto targetHistoryListDataDto = new TargetHistoryListDataDto();
                        targetHistoryListDataDto.setRegistRationId(item.getLaRegistRationId());
                        targetHistoryListDataDto.setEventId(item.getEventId());
                        if("01".equals(item.getActiveRange())){
                            targetHistoryListDataDto.setConsNo("O"+item.getRangeNo());
                        }
                        else{
                            targetHistoryListDataDto.setConsNo(item.getRangeNo());
                        }
                        targetHistoryListDataDto.setOrgNo("13101");
                        targetHistoryListDataDto.setResponseType(responseEvent.getReponseType());
                        targetHistoryListDataDto.setActiveStartTime1(responseEvent.getActiveStartTime1());
                        targetHistoryListDataDto.setActiveEndTime1(responseEvent.getActiveEndTime1());
                        if(responseEvent.getActiveStartTime1()!=null){
                            targetHistoryListDataDto.setActiveCount1(12.34);
                        }
                        targetHistoryListDataDto.setActiveStartTime2(responseEvent.getActiveStartTime2());
                        targetHistoryListDataDto.setActiveEndTime2(responseEvent.getActiveEndTime2());
                        if(responseEvent.getActiveStartTime2()!=null){
                            targetHistoryListDataDto.setActiveCount2(23.45);
                        }
                        data.add(targetHistoryListDataDto);
                    });

                    targetHistoryDto.setData(data);
                }
            }
            else{
                targetHistoryDto.setStatus("01");
                List<TargetHistoryListDataDto> data = new ArrayList<>();
                TargetHistoryListDataDto targetHistoryListDataDto_1 = new TargetHistoryListDataDto();
                targetHistoryListDataDto_1.setRegistRationId(targetHistoryRequest.getRegistRationId());
                targetHistoryListDataDto_1.setEventId(targetHistoryRequest.getEventId());
                targetHistoryListDataDto_1.setConsNo("consNo01");
                targetHistoryListDataDto_1.setOrgNo("13401");
                targetHistoryListDataDto_1.setResponseType("01");
                targetHistoryListDataDto_1.setActiveStartTime1(LocalDateTimeUtil.getDateString(new Date(), ConstantParameter.dateFormatyyyyMMddHHmmss));
                targetHistoryListDataDto_1.setActiveEndTime1(LocalDateTimeUtil.getDateString(new Date(), ConstantParameter.dateFormatyyyyMMddHHmmss));
                targetHistoryListDataDto_1.setActiveCount1(12.34);
                targetHistoryListDataDto_1.setActiveStartTime2(LocalDateTimeUtil.getDateString(new Date(), ConstantParameter.dateFormatyyyyMMddHHmmss));
                targetHistoryListDataDto_1.setActiveEndTime2(LocalDateTimeUtil.getDateString(new Date(), ConstantParameter.dateFormatyyyyMMddHHmmss));
                targetHistoryListDataDto_1.setActiveCount2(23.45);
                data.add(targetHistoryListDataDto_1);
                TargetHistoryListDataDto targetHistoryListDataDto_2 = new TargetHistoryListDataDto();
                targetHistoryListDataDto_2.setRegistRationId(targetHistoryRequest.getRegistRationId());
                targetHistoryListDataDto_2.setEventId(targetHistoryRequest.getEventId());
                targetHistoryListDataDto_2.setConsNo("consNo02");
                targetHistoryListDataDto_2.setOrgNo("13401");
                targetHistoryListDataDto_2.setResponseType("01");
                targetHistoryListDataDto_2.setActiveStartTime1(LocalDateTimeUtil.getDateString(new Date(), ConstantParameter.dateFormatyyyyMMddHHmmss));
                targetHistoryListDataDto_2.setActiveEndTime1(LocalDateTimeUtil.getDateString(new Date(), ConstantParameter.dateFormatyyyyMMddHHmmss));
                targetHistoryListDataDto_2.setActiveCount1(22.22);
                targetHistoryListDataDto_2.setActiveStartTime2(LocalDateTimeUtil.getDateString(new Date(), ConstantParameter.dateFormatyyyyMMddHHmmss));
                targetHistoryListDataDto_2.setActiveEndTime2(LocalDateTimeUtil.getDateString(new Date(), ConstantParameter.dateFormatyyyyMMddHHmmss));
                targetHistoryListDataDto_2.setActiveCount2(33.33);
                data.add(targetHistoryListDataDto_2);
                targetHistoryDto.setData(data);
            }
        }
        return GsonUtils.toJsonWtihNullField(targetHistoryDto);
    }

    @Override
    public String upLoadUserInfo(String jsonParameter) {

        UpLoadUserInfoRequest upLoadUserInfoRequest = GsonUtils.fromJson(jsonParameter,new TypeToken<UpLoadUserInfoRequest>(){}.getType());

        UpLoadUserInfoDto upLoadUserInfoDto = new UpLoadUserInfoDto();

        User user = serverMapper.selectUserByID(upLoadUserInfoRequest.getRegistRationId());
        if(user==null){
            log.error("注册ID{}用户不存在", upLoadUserInfoRequest.getRegistRationId());
            upLoadUserInfoDto.setStatus("02");
            return GsonUtils.toJsonWtihNullField(new CommonResponseDTO("02", "注册ID用户不存在"));
        }
        else{
            log.info("运行数据-> {}", upLoadUserInfoRequest.toString());
            boolean verifyPara = true; //是否验证参数
            boolean addConsNotExist = true; //向用户档案表增加不存在用户
            boolean addResponseConsNotExist = true; //向用户档案表增加不存在用户
            boolean updateResponseConsPass = false; //
            if(verifyPara){
                upLoadUserInfoDto.setStatus("01");
                if(!CollectionUtils.isEmpty(upLoadUserInfoRequest.getData())){
                    List<String> consNoParaList = new ArrayList<>();
                    upLoadUserInfoRequest.getData().forEach(item ->{
                        if(!consNoParaList.contains(item.getCONS_NO())){
                            consNoParaList.add(item.getCONS_NO());
                        }
                    });
                    if(addConsNotExist){
                        List<String> consNoExistList = getConsNoListFromCons(consNoParaList);
                        if(consNoExistList==null || consNoExistList.size()!=consNoParaList.size()){
                            log.error("增加无档案用户");
                            List<String> consNoAddList = new ArrayList<>();
                            consNoAddList.addAll(consNoParaList);
                            if(consNoExistList!=null){
                                consNoAddList.removeAll(consNoExistList);
                            }
                            addConsNoListToCons(consNoAddList);
                        }
                    }
                    List<ResponseCons> responseConsList = new ArrayList<>();
                    if (!CollectionUtils.isEmpty(consNoParaList)) {
                        List<List<String>> objListList = ObjConvertListUtil.splitArrayList(consNoParaList, 800);
                        for(List<String> objList:objListList){
                            List<ResponseCons> resultListTmp = serverMapper.getResponseConsList(upLoadUserInfoRequest.getRegistRationId(), objList);
                            if (!CollectionUtils.isEmpty(resultListTmp)) {
                                responseConsList.addAll(resultListTmp);
                            }
                        }
                    }

                    List<String> canRespConsNoList = new ArrayList<>();
                    List<String> waitRespConsNoList = new ArrayList<>();
                    List<String> canNotRespConsNoList = new ArrayList<>();
                    List<String> consNoNotExistList = new ArrayList<>();
                    consNoNotExistList.addAll(consNoParaList);
                    if(!CollectionUtils.isEmpty(responseConsList)){
                        for(ResponseCons responseCons:responseConsList){
                            if(Integer.valueOf(1).equals(responseCons.getConsStatus())){
                                canRespConsNoList.add(responseCons.getConsNo());
                            }
                            else if(Integer.valueOf(0).equals(responseCons.getConsStatus())){
                                if(responseCons.getConsNo().length()==10 && responseCons.getConsNo().startsWith("0")){
                                    waitRespConsNoList.add(responseCons.getConsNo());
                                }
                            }
                            else{
                                canNotRespConsNoList.add(responseCons.getConsNo());
                            }
                            consNoNotExistList.remove(responseCons.getConsNo());
                        }
                        if(updateResponseConsPass){
                            if(!CollectionUtils.isEmpty(waitRespConsNoList)){
                                log.error("更新响应负荷用户");
                                //更新为审核通过 //TODO
                                if(!CollectionUtils.isEmpty(waitRespConsNoList)){
                                    List<List<String>> objListList = ObjConvertListUtil.splitArrayList(waitRespConsNoList, 800);
                                    for(List<String> objList:objListList){
                                        serverMapper.updateRespondConsNoListPass(upLoadUserInfoRequest.getRegistRationId(), objList);
                                    }
                                }
                                canRespConsNoList.addAll(waitRespConsNoList);
                            }
                        }
                    }
                    if(addResponseConsNotExist){
                        if(!CollectionUtils.isEmpty(consNoNotExistList)){
                            log.error("增加无档案响应负荷用户");
                            List<ReportResourceListDataRequest> addList = new ArrayList<>();
                            List<String> consNoAddList = new ArrayList<>();
                            for(String consNo:consNoNotExistList){
                                if(consNo.length()==10 && consNo.startsWith("0")){
                                    ReportResourceListDataRequest reportResourceListDataRequest = new ReportResourceListDataRequest();
                                    reportResourceListDataRequest.setConsNo(consNo);
                                    consNoAddList.add(consNo);
                                    addList.add(reportResourceListDataRequest);
                                }
                                else{
                                    canNotRespConsNoList.add(consNo);
                                }
                            }
                            if(!CollectionUtils.isEmpty(addList)){
                                addRespondConsList(upLoadUserInfoRequest.getRegistRationId(), addList, updateResponseConsPass?"1":"0");
                                if(updateResponseConsPass){
                                    canRespConsNoList.addAll(consNoAddList);
                                }
                            }
                        }
                    }
                    List<UpLoadUserInfoListDataDto> data = new ArrayList<>();
                    upLoadUserInfoRequest.getData().forEach(item ->{
                        UpLoadUserInfoListDataDto upLoadUserInfoListDataDto = new UpLoadUserInfoListDataDto();
                        upLoadUserInfoListDataDto.setCONS_NO(item.getCONS_NO());
                        if(canRespConsNoList!=null && canRespConsNoList.contains(item.getCONS_NO())){
                            upLoadUserInfoListDataDto.setHPLC("01");
                            upLoadUserInfoListDataDto.setVER_STATUS("01");
                            data.add(upLoadUserInfoListDataDto);
                        }
                        else if(canNotRespConsNoList.contains(item.getCONS_NO())){
                            upLoadUserInfoListDataDto.setHPLC("00");
                            upLoadUserInfoListDataDto.setVER_STATUS("02");
                            data.add(upLoadUserInfoListDataDto);
                        }
                    });
                    upLoadUserInfoDto.setData(data);
                }
            }
            else{
                upLoadUserInfoDto.setStatus("01");
                if(!CollectionUtils.isEmpty(upLoadUserInfoRequest.getData())){
                    List<UpLoadUserInfoListDataDto> data = new ArrayList<>();
                    upLoadUserInfoRequest.getData().forEach(item ->{
                        UpLoadUserInfoListDataDto upLoadUserInfoListDataDto = new UpLoadUserInfoListDataDto();
                        upLoadUserInfoListDataDto.setCONS_NO(item.getCONS_NO());
                        upLoadUserInfoListDataDto.setHPLC("01");
                        data.add(upLoadUserInfoListDataDto);
                    });
                    upLoadUserInfoDto.setData(data);
                }
            }
        }

        return GsonUtils.toJsonWtihNullField(upLoadUserInfoDto);
    }
}
